// pages/code/code.js
var app = getApp();
var utils = require('../../utils/util.js');
var method = require('../../utils/function.js');
Page({

    /**
     * 页面的初始数据
     */
	data: {
		curHdIndex: 0,
		qrcodeValue1: "",
		qrcodeValue2: "",
		canvasHidden: true, //设置画板的显示与隐藏，画板不隐藏会影响页面正常显示
	},

    /**
     * 生命周期函数--监听页面加载
     */
	onLoad: function (options) {
		// 邀请用户的二维码地址: https://breakfast.qidian2018.com/inv_member.html?inv_id=
		// 邀请店铺的二维码地址: https://breakfast.qidian2018.com/inv_store.html?inv_id=
		this.personalInfo();
		// this.setData({
		// 	inv_id : options.q ? options.q : null,
		// })
	},
	//tab切换
	tab: function (e) {
		var dataId = e.currentTarget.id;
		var curHdIndex = dataId;
		this.setData({
			curHdIndex: curHdIndex
		})
	},
	personalInfo: function (keyword) {
		var that = this;
		utils.http("/api/v1/member/index", {
		}, function (res) {
			if (res.state == 1) {
				var list = [
					{
						id: 0,
						name: "六卿早餐到宿舍",
						name1: "餐餐惊喜红包拿",
						qrcodeValue: "https://www.tglq.top/inv.html?inv_id=" + res.datas.id
					}, {
						id: 1,
						name: "六卿在手",
						name1: "订单爆走",
						qrcodeValue: "https://www.tglq.top/inv_store.html?inv_id=" + res.datas.id
					}
				]
				that.setData({
					info: res.datas,
					list: list
				})


			} else {
				utils.message(res.datas)
			}
		})
	},
	//保存图片
	bindDaveTap: function () {
		console.log(this.data.curHdIndex)

	},
	bindDaveTap: function (e) {
		utils.showLoading('图片生成中');
		var that = this;
		that.setData({
			ewmHidden: false
		}, function () {
			const thatssa = that.selectComponent('#qrcode')
			console.log('thatssa', thatssa)
			wx.canvasToTempFilePath({
				canvasId: 'wux-qrcode',
				width: 375,
				height: 533,
				destWidth: 375 * 2,
				destHeight: 533 * 2,
				success: function (res) {
					that.setData({
						rwmImg: res.tempFilePath,
					}, function () {
						that.setData({
							canvasHidden: false
						})
						console.log(this.data.rwmImg)
						const systemInfo = wx.getSystemInfoSync()
						wx.getSystemInfo({
							success: res => {
								console.log(res);
								that.setData({
									screenWidth: res.screenWidth,
									screenHeight: res.screenHeight
								})
							}
						})
						var unit = that.data.screenWidth / 375;
						var butback = "/images/code.png";
						const ctx = wx.createCanvasContext('starkImg', that);
						//红色背景
						method.fillRoundRect(ctx, 0, 0, 375, 533,0, '#FF7350');
						ctx.drawImage(butback,50, 90, 275, 333);
						console.log(that.data.list[that.data.curHdIndex])
						var price = that.data.list[that.data.curHdIndex].name;
						ctx.setFontSize(28)
						ctx.setFillStyle("#fd5c33");
						ctx.fillText(price, 118, 146, 200);
						var price1 = that.data.list[that.data.curHdIndex].name1;
						ctx.setFontSize(28)
						ctx.setFillStyle("#fd5c33");
						ctx.fillText(price1,95, 190, 200);
						//二维码
						ctx.drawImage(that.data.rwmImg,104,214,176,170);
						ctx.draw();
						//将生成好的图片保存到本地，需要延迟一会，绘制期间耗时
						setTimeout(function () {
							utils.showLoading('保存中');
							wx.canvasToTempFilePath({
								canvasId: 'starkImg',
								success: function (res) {
									var tempFilePath = res.tempFilePath;
									that.setData({
										imagePath: tempFilePath,
										canvasHidden: true
									});

									if (!res.tempFilePath) {
										wx.showModal({
											title: '提示',
											content: '图片绘制中，请稍后重试',
											showCancel: false
										})
									}
									//画板路径保存成功后，调用方法吧图片保存到用户相册
									wx.saveImageToPhotosAlbum({
										filePath: res.tempFilePath,
										//保存成功失败之后，都要隐藏画板，否则影响界面显示。
										success: (res) => {
											//  console.log(res)
											wx.hideLoading()
											wx.showToast({
												title: '保存成功',
											})
											that.setData({
												canvasHidden: true,
												ewmHidden: true,
											})
										},
										fail: (err) => {
											// console.log(err)
											wx.hideLoading()
											that.setData({
												canvasHidden: true,
												ewmHidden: true,
											})
										}
									})
								},
								fail: function (res) {
									console.log('保存失败', res);
								}
							}, that);
						}, 200);
					});
				},
				fail: function (err) {
					console.log('二维码错啦！？？？', err);
				},
			}, thatssa)
		})
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
	onReady: function () {

	},

    /**
     * 生命周期函数--监听页面显示
     */
	onShow: function () {

	},

    /**
     * 生命周期函数--监听页面隐藏
     */
	onHide: function () {

	},

    /**
     * 生命周期函数--监听页面卸载
     */
	onUnload: function () {

	},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
	onPullDownRefresh: function () {

	},

    /**
     * 页面上拉触底事件的处理函数
     */
	onReachBottom: function () {

	},

    /**
     * 用户点击右上角分享
     */
	onShareAppMessage: function () {

	}
})